package cn.exrick.common.zhima;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.ZhimaCustomerCertificationCertifyRequest;
import com.alipay.api.request.ZhimaCustomerCertificationInitializeRequest;
import com.alipay.api.request.ZhimaCustomerCertificationQueryRequest;
import com.alipay.api.response.ZhimaCustomerCertificationCertifyResponse;
import com.alipay.api.response.ZhimaCustomerCertificationInitializeResponse;
import com.alipay.api.response.ZhimaCustomerCertificationQueryResponse;

import cn.exrick.common.alipay.AlipayService;
import cn.exrick.pojo.front.ZhimaResult;
import cn.exrick.utils.IDUtil;

@Service
public class ZhimaCertificatiionService {

	private Logger log = LoggerFactory.getLogger(AlipayService.class);

	@Value("${ALIPAY_APP_ID}")
	public String appId;
	// 私钥
	@Value("${ALIPAY_APP_PRIVATE_KEY}")
	public String privateKey;
	// 秘钥
	@Value("${ALIPAY_PUBLIC_KEY}")
	public String publicKey;

	public ZhimaResult init(String certUserName, String certNum) {
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey,
				"json", "utf-8", publicKey, "RSA2");

		ZhimaCustomerCertificationInitializeRequest request = new ZhimaCustomerCertificationInitializeRequest();
		JSONObject biz_content = new JSONObject();
		biz_content.put("transaction_id", IDUtil.zhimaTransactionId());
		biz_content.put("product_code", "w1010100000000002978");
		biz_content.put("biz_code", "FACE");
		JSONObject identity_param = new JSONObject();
		identity_param.put("identity_type", "CERT_INFO");
		identity_param.put("cert_type", "IDENTITY_CARD");
		identity_param.put("cert_name", certUserName);
		identity_param.put("cert_no", certNum);
		biz_content.put("identity_param", identity_param.toJSONString());

		request.setBizContent(biz_content.toString());

		ZhimaResult result = new ZhimaResult();
		try {
			ZhimaCustomerCertificationInitializeResponse response = alipayClient.execute(request);
			if (response.isSuccess()) {
				result.setSuccess(true);
				result.setInitSuccessNo(response.getBizNo());
			} else {
				result.setSuccess(false);
				result.setErrMsg(response.getSubMsg());
			}
		} catch (AlipayApiException e) {
			result.setSuccess(false);
			result.setErrMsg(e.getErrMsg());
			log.error("用户芝麻认证初始化失败！用户名：{}，证件号：{}, 失败信息：{}", certUserName, certNum, e.getMessage());
		}
		return result;
	}

	public ZhimaResult certify(String bizNo) {
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey,
				"json", "utf-8", publicKey, "RSA2");
		ZhimaCustomerCertificationCertifyRequest request = new ZhimaCustomerCertificationCertifyRequest();
		JSONObject biz_content = new JSONObject();
		biz_content.put("biz_no", bizNo);
		request.setBizContent(biz_content.toString());
		
		ZhimaResult result = new ZhimaResult();
		try {
			ZhimaCustomerCertificationCertifyResponse response = alipayClient.pageExecute(request,"GET");
			if(null == response.getFailedReason()) {
				result.setSuccess(true);
				result.setUrl(response.getBody());
			}else {
				result.setSuccess(false);
				result.setErrMsg(response.getFailedReason());
			}
		} catch (AlipayApiException e) {
			result.setSuccess(false);
			result.setErrMsg(e.getErrMsg());
			log.error("用户芝麻认证发起认证失败！认证号：{}, 失败信息：{}", bizNo, e.getMessage());
		}
		return result;
	}
	
	public ZhimaResult query(String bizNo) {
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey,
				"json", "utf-8", publicKey, "RSA2");
		ZhimaCustomerCertificationQueryRequest request = new ZhimaCustomerCertificationQueryRequest();
		JSONObject biz_content = new JSONObject();
		biz_content.put("biz_no", bizNo);
		request.setBizContent(biz_content.toString());
		
		ZhimaResult result = new ZhimaResult();
		try {
			ZhimaCustomerCertificationQueryResponse response = alipayClient.execute(request);
			if("true".equals(response.getPassed())) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
				result.setErrMsg(response.getFailedReason());
			}
		} catch (AlipayApiException e) {
			result.setSuccess(false);
			result.setErrMsg(e.getErrMsg());
			log.error("用户芝麻认证发起认证失败！认证号：{}, 失败信息：{}", bizNo, e.getMessage());
		}
		return result;
	}


}
